package com.edwin.preventscreenlock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.event.KeyEvent;

public class App {

    private static Logger logger = LoggerFactory.getLogger(App.class);

    public static final int minS = 30;
    public static final int maxS = 60;

    private static int lastX = 0;
    private static int lastY = 0;

    public static void main(String[] args) {
        logger.info("开始执行防止屏幕lock");
        while (true) {
            logger.info("执行操作");
            try {
                PointerInfo mouseLocation = MouseInfo.getPointerInfo();
                int x = mouseLocation.getLocation().x;
                int y = mouseLocation.getLocation().y;
                System.out.println("X:" + x + " Y:" + y);
                System.out.println("lastX:" + lastX + " lastY:" + lastY);

                if (lastX == x && lastY == y) {
                    mouseMoveToXY(x, y);
                }

                lastX = x;
                lastY = y;

                Thread.sleep((int) (Math.random() * (maxS - minS) + minS) * 1000);
            } catch (Exception e) {
                e.printStackTrace();
                pressSingleKeyByNumber(KeyEvent.VK_PAUSE);
                continue;
            }
        }
    }

    public static final void pressSingleKeyByNumber(int keycode) {
        try {
            Robot robot = new Robot();
            robot.keyPress(keycode);
            robot.keyRelease(keycode);
            robot.delay(500);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public static final void mouseMoveToXY(int x, int y) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(x + 1, y + 1);
            robot.mouseMove(x, y);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}
